import loginView from '$v/login'
import { loginRequest } from '../api/admin'
export default (req, res) => {
    res.render(loginView())
    //点击登录按钮逻辑
  document.loginForm.loginBtn.onclick = async function (e) {
    e.preventDefault();

    //获取输入的用户名和密码
    const adminNameValue = document.loginForm.adminName.value;
    const passwordValue = document.loginForm.passWord.value;
    // console.log(adminNameValue, passwordValue);

    //登录请求
    try {
      const loginResult = await loginRequest({
        adminName: adminNameValue,
        password: passwordValue
      })
      //登录成功可以拿到token,把token保存在localStorage中,并跳转到首页
      localStorage.setItem("user_token",loginResult.token)
      router.go("/adv/adminList")
    } catch (e) {
      alert("登录异常" + e.message)
    }
  } 
}